package com.deng.commons.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * 接口数据返回的基类，包涵每个接口通用格式
 * Object： 会进行强制类型转换
 *      Object在接口文档中无法展示数据格式
 */
@Data
@ApiModel
@NoArgsConstructor
@AllArgsConstructor
public class BaseResult<T> {

    @ApiModelProperty("统一返回码")
    private Integer code;

    @ApiModelProperty("统一返回消息")
    private String msg;

    @ApiModelProperty("接口返回数据")
    private T data;


    public BaseResult(ErrorCode rc) {
        this.code = rc.getCode();
        this.msg = rc.getMsg();
    }

    public BaseResult(ErrorCode rc,T data) {
        this.code = rc.getCode();
        this.msg = rc.getMsg();
        this.data  = data;
    }

    /**
     * 返回成功
     * @return
     */
    public static BaseResult success(){
        return new BaseResult(ErrorCode.CODE_200);
    }

    public static <T> BaseResult success(T data){
        return new BaseResult(ErrorCode.CODE_200,data);
    }

    /**
     * 返回失败结果
     * @return
     */
    public static BaseResult fail(){
        return new BaseResult(ErrorCode.CODE_400);
    }

    public static BaseResult fail(ErrorCode errorCode){
        return new BaseResult(errorCode);
    }

    /**
     * 根据布尔值返回结果
     * @param flag
     * @return
     */
    public static BaseResult result(boolean flag){
        if (flag){
            return success();
        }else{
            return fail();
        }
    }

}
